﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DevicePropertyHandlers.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2009
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DeviceManagement
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Runtime.InteropServices;

	internal static class DevicePropertyHandlers
	{
		public static Guid ParseGuid(IntPtr pBuffer, uint bufferLength)
		{
			return (Guid)Marshal.PtrToStructure(pBuffer, typeof(Guid));
		}

		public static string ParseString(IntPtr pBuffer, uint bufferLength)
		{
			return Marshal.PtrToStringAuto(pBuffer);
		}

		public static IEnumerable<string> ParseStringList(IntPtr pBuffer, uint propertyBufferSize)
		{
			int stringLength = ((int)propertyBufferSize) / Marshal.SizeOf(typeof(short));

			string s = Marshal.PtrToStringAuto(pBuffer, stringLength);

			return s.Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries).ToList().AsReadOnly();
		}

		public static uint ParseUInt32(IntPtr pBuffer, uint bufferLength)
		{
			return (uint)Marshal.ReadInt32(pBuffer);
		}

		internal static bool ParseBoolean(IntPtr p, uint bufferLength)
		{
			if (bufferLength == 1)
			{
				var value = (int)Marshal.ReadByte(p);
				return value != 0 ? true : false;
			}

			return false;
		}
	}
}